﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CommerceOne.Domains.Commons
{
    public class FilterParam<T> : IFilterParam
    {
        private List<T > paramList = new List<T>();

        public string FieldName { get; set; }

        public string ObjectName { get; set; }

        public FilterType Type { get; set; }

        public void AddValue(object value)
        {
            T t = (T) value;
            this.paramList.Add(t);
        }

        public FilterParam(string filterName, string objectName, FilterType type)
        {
            this.FieldName = filterName;
            this.ObjectName = objectName;
            this.Type = type;
        }

        public FilterParam(string filterName, string objectName, FilterType type, object value) : this(filterName, objectName, type)
        {
          this.AddValue(value);
        }

        public override string ToString()
        {
            if(string.IsNullOrEmpty(this.ObjectName)) return string.Empty;
            string str = this.ObjectName;
            if(!string.IsNullOrEmpty(this.FieldName)) str = this.FieldName + "." + this.ObjectName;
      
            switch (this.Type)
            {
                case FilterType.Like:
                    if (this.paramList.Count == 0) return string.Empty;
                    return string.Format(" ( {0} LIKE '%{1}' OR {0} LIKE '{1}%' OR {0} LIKE '%{1}%' )", str, this.paramList[0]);

                case FilterType.Smaller:
                    if (this.paramList.Count == 0) return string.Empty;
                    return string.Format(" {0} < {1}", str, this.paramList[0]);

                case FilterType.Greater:
                    if (this.paramList.Count == 0) return string.Empty;
                    return string.Format(" {0} > {1}", str, this.paramList[0]);

                case FilterType.Equal:
                    if (this.paramList.Count == 0) return string.Empty;
                    return string.Format(" {0} = {1}", str, this.paramList[0]);

                case FilterType.SmallerEqual:
                    if (this.paramList.Count == 0) return string.Empty;
                    return string.Format(" {0} <= {1}", str, this.paramList[0]);

                case FilterType.GreaterEqual:
                    if (this.paramList.Count == 0) return string.Empty;
                    return string.Format(" {0} >= {1}", str, this.paramList[0]);

                case FilterType.In:
                    if (this.paramList.Count == 0) return string.Empty;
                    string valueIn = string.Join(" ,", this.paramList);
                    if (string.IsNullOrEmpty(valueIn)) return string.Empty;
                    return string.Format(" {0} IN ({1})", str, valueIn);

                case FilterType.NotIn:
                    if (this.paramList.Count == 0) return string.Empty;
                    string valueNotIn = string.Join(" ,", this.paramList);
                    if (string.IsNullOrEmpty(valueNotIn)) return string.Empty;
                    return string.Format(" {0} NOT IN ({1})", str, valueNotIn);

                case FilterType.IsNull:
                    return string.Format(" {0} IS NULL", str);

                case FilterType.NotNull:
                    return string.Format(" {0} IS NOT NULL)", str);

                default:
                    return string.Empty;
            }
        }
    }
}
